﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_CountryEdit : System.Web.UI.Page
{
    CountryBusiness cb = new CountryBusiness();
    private int id = Helper.getQuery("countryID");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CountryEntities ce = cb.getCountry(id);
            bindDDl();
            if (id > 0)
            {
                btnSubmit.Text = "Sửa";
                txtName.Text = ce._countryName;
                txtSymbol.Text = ce._countrySymbol;
                txtSummary.Text = ce._countrySummary;
                txtInfo.Text = ce._countryInfo;
                Label1.Text = viewImage(id, "100", "100");
            }
            else
                btnSubmit.Text = "Thêm mới";
        }
    }
    public string viewImage(int id, string width, string height)
    {
        return "<img src='/eprojectAirlinesReservationSystem/admin/ImageFrame.aspx?countryImage=" + id + "' Width=" + width + " Height=" + height + "/>";
    }

    public void bindDDl()
    {
        PanelBusiness pb = new PanelBusiness();
        ddlPanel.DataSource = pb.getAllPanel();
        ddlPanel.DataTextField = "pannelHeader";
        ddlPanel.DataValueField = "panelID";
        ddlPanel.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CountryEntities ce = new CountryEntities();
        ce._countryID = id;
            ce._countrySymbol = txtSymbol.Text;
            ce._countryName = txtName.Text;
            ce._countrySummary = txtSummary.Text;
            ce._countryInfo = txtInfo.Text;
            ce._panelID = int.Parse(ddlPanel.SelectedValue);
            ce._countryImage = new byte[uploadImage.PostedFile.ContentLength];
            uploadImage.PostedFile.InputStream.Read(ce._countryImage, 0, ce._countryImage.Length);
            if (btnSubmit.Text == "Thêm mới")
            {

                if (cb.Insert(ce))
                    Response.Redirect("Country.aspx");
                else
                    Label2.Text = "Chưa thêm được";
            }
            else
            {
                if (cb.Edit(ce, true))
                    Response.Redirect("Country.aspx");
                else
                    Label2.Text = "Chưa sửa được";
            }
    }
}